var app = getApp();
Page({
  data: {
    amount:'',
    cardName:'',
    cardIdentity:'',
    cardType:'',
    cardNumber:'',
    valideCode: '', 
    time: "获取验证码",
    currentTime: 61,
    disabled: false,
    suffix: '',
    dataList:[],
    UserInfo:{},
  },
  valideCodeInput(e) {
    this.setData({
      valideCode: e.detail.value
    })
  },
  sendCode() {
    let _this = this;
    if (!_this.data.disabled) {
      _this.getCode();
    }
  },

  getCode() {
    let _this = this; 
    app.request({
      url: 'api/wechat/sendCodeTixian',
      method: "GET",
      success: function (res, header) {
        if (header != null){
          // var index1 = header["Set-Cookie"].indexOf("JSESSIONID")
          // var index2 = header["Set-Cookie"].indexOf(";", index1)
          // var JSESSIONID = header["Set-Cookie"].substring(index1, index2)
          // wx.setStorageSync('cookie', wx.getStorageSync("cookie") + ";" + JSESSIONID)
        }

        wx.showToast({
          title: '已发送'
        })
        _this.setData({
          disabled: true
        })
        let interval = null;
        let currentTime = _this.data.currentTime;

        interval = setInterval(function () {
          currentTime--;
          _this.setData({
            time: currentTime,
            suffix: '秒后可重新获取'
          })
          if (currentTime <= 0) {
            clearInterval(interval)
            _this.setData({
              time: '重新发送',
              suffix: '',
              currentTime: 61,
              disabled: false
            })
          }
        }, 1000)
      }
    })
  },
  cardIdentityInput(e) {
    this.setData({
      cardIdentity: e.detail.value
    })
  },
  cardTypeInput(e){
    this.setData({
      cardType: e.detail.value
    })
  },
  clearInputEvent: function (res) {
    this.setData({
      'amount': ''
    })
  },
  amountInput(e){
    this.setData({
      amount:e.detail.value
    })
  },
  dealNumber(money) {
    if (money && money != null) {
      money = String(money);
      var left = money.split('.')[0], right = money.split('.')[1];
      right = right ? (right.length >= 2 ? '.' + right.substr(0, 2) : '.' + right + '0') : '.00';
      var temp = left.split('').reverse().join('').match(/(\d{1,3})/g);
      return (Number(money) < 0 ? "-" : "") + temp.join(',').split('').reverse().join('') + right;
    } else if (money === 0) {   //注意===在这里的使用，如果传入的money为0,if中会将其判定为boolean类型，故而要另外做===判断
      return '0.00';
    } else {
      return "";
    }
  },
  cardNameInput(e) {
    this.setData({
      cardName: e.detail.value
    })
  },
  cardNumberInput(e){
    this.setData({
      cardNumber: e.detail.value
    })
  },
  shenqing(){
    let self = this
   
    if (this.data.amount == '' || isNaN(this.data.amount) || this.data.amount<= 0) {
      wx.showToast({
        title: '提现金额错误，请检查！',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (parseFloat(this.data.amount) > parseFloat(this.data.UserInfo.userBalance)){
      wx.showToast({
        title: '申请金额不能大于用户余额！',
        icon: 'none',
        duration: 3000
      })
      this.setData({
        amount:this.data.UserInfo.userBalance
      })
      return;
    }


    if (this.data.cardName == '') {
      wx.showToast({
        title: '请输入持卡人姓名',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (this.data.cardIdentity == '') {
      wx.showToast({
        title: '请输入身份证号',
        icon: 'none',
        duration: 3000
      })
      return;
    }

    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (!reg.test(this.data.cardIdentity)) {
      wx.showToast({
        title: '身份证格式错误',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    if (this.data.cardNumber == '') {
      wx.showToast({
        title: '请输入银行卡号',
        icon: 'none',
        duration: 3000
      })
      return;
    }
    var pattern = /^\d{16,19}$/
    if(!pattern.test(this.data.cardNumber)) {
      wx.showToast({
        title: '请输入正确的银行卡号',
        icon: 'none',
        duration: 3000
      })
      return;
    }

    if (this.data.cardType == '') {
      wx.showToast({
        title: '请输入开户行名称',
        icon: 'none',
        duration: 3000
      })
      return;
    }

    app.request({
      url: 'api/wechat/tixian',
      method: "GET",
      data:{
        amount: self.data.amount,
        cardName: self.data.cardName,
        cardIdentity: self.data.cardIdentity,
        cardType: self.data.cardType,
        cardNumber: self.data.cardNumber,
        code: self.data.valideCode,
      },
      success: function (res) {
        if(res.data.result){
          wx.navigateTo({
            url: '/page/UserCenter/success/index?cardNumber=' + self.data.cardNumber + "&amount=" + self.data.amount + "&cardType=" + self.data.cardType,
          })
        } else {
          wx.showToast({ title: res.data.errMsg, icon: "none", duration: 3000 });
        }
      }
    })
  },
  onShow: function () {
    let self = this
    //账户信息
    app.request({
      url: 'api/wechat/getUserInfo',
      method: "GET",
      success: function (res) {
        res.data.info.userBalanceText = self.dealNumber(res.data.info.userBalance)
        self.setData({
          UserInfo: res.data.info
        })
      }
    })
    app.request({
      url: 'api/wechat/getLastTixian',
      method: "GET",
      success: function (res) {
        if (res.data.result) {
          var row = res.data.info
          self.setData({
            cardName: row.cardName,
            cardIdentity: row.cardIdentity,
            cardType: row.cardType,
            cardNumber: row.cardNumber
          })
        }
      }
    })
    // this.getBankCardList()
  },
  viewDetail(e){
    var id = e.currentTarget.dataset.id
    console.log(id)
    wx.navigateTo({
      url: '/page/UserCenter/addBankCard/index?id='+id
    })
  },
  getBankCardList(){
    let self = this
    app.request({
      url: 'api/wechat/getBankCardByUserId',
      method: "GET",
      success: function (res) {
        console.log(res.data)
        if (res.data != null && res.data.length > 0) {
          self.setData({
            cardId:res.data[0].id,
            cardNumber: res.data[0].cardNumber,
            cardType: res.data[0].cardType,
            dataList:res.data
          })
        }else{
          self.setData({
            dataList:[]
          })
        }
      }
    })
  }
})